﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Tour_TourCategoryAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            this.SetLabelLanguage();
            ValidateControl();
            BindDataToLanguageDropDown();
            BindDataToCountryDropdown();
            BindDataWhenEdit();
        }
    }

    #region Relative Functions
    private void ValidateControl()
    {
        int nCurrentLang = Support.GetCurrentLanguage();
        rfvTourCategoryname.ErrorMessage = MessagesValidate.DestinationTour_EMPTY[nCurrentLang - 1];
    }
    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        lblCountry.Text = MultipleLanguage.LBL_COUNTRY[nDefaultLanguage - 1];
        lblRegion.Text = MultipleLanguage.REGION[nDefaultLanguage - 1];
        lblContent.Text = MultipleLanguage.LBL_CONTENT[nDefaultLanguage - 1];
        lblImage.Text = MultipleLanguage.LBL_IMAGE[nDefaultLanguage - 1];
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];
        lblTourCategoryName.Text = MultipleLanguage.TOURCATEGORY[nDefaultLanguage - 1];
        btnBack.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage - 1];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage - 1];
    }
    private void BindDataWhenEdit()
    {
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);

        string sAction = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        try
        {
            if (sAction == Config.ACTION_EDIT)
            {
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                if (nID > 0)
                {
                    TourCategory objTourCategory = new TourCategory();
                    objTourCategory.LoadById(nID);

                    int nCountry = 0;
                    int nRegion = -1;
                    if (objTourCategory.Data.RegionID > 0)
                    {
                        Region objRegion = new Region();
                        objRegion.LoadById(objTourCategory.Data.RegionID);
                        ddlCountry.SelectedValue = ConvertData.ConvertToString(objRegion.Data.CountryID);
                        nCountry = objRegion.Data.CountryID;
                        nRegion = objTourCategory.Data.RegionID;
                    }
                    BindDataToRegionDropdown(nCountry);
                    ddlRegion.SelectedValue = ConvertData.ConvertToString(nRegion);

                    switch (nCurrentLanguage)
                    {
                        case 1:
                            txtTourCategoryName.Text = ConvertData.ConvertToString(objTourCategory.Data.TourCategoryName1);
                            FTBContent.IntialText(ConvertData.ConvertToString(objTourCategory.Data.TourCategoryDetail1));
                            break;
                        case 2:
                            txtTourCategoryName.Text = ConvertData.ConvertToString(objTourCategory.Data.TourCategoryName2);
                            FTBContent.IntialText(ConvertData.ConvertToString(objTourCategory.Data.TourCategoryDetail2));
                            break;
                        case 3:
                            txtTourCategoryName.Text = ConvertData.ConvertToString(objTourCategory.Data.TourCategoryName3);
                            FTBContent.IntialText(ConvertData.ConvertToString(objTourCategory.Data.TourCategoryDetail3));
                            break;
                    }

                    if (objTourCategory.Data.Images != "")
                        imgImage.ImageUrl = Config.ROOT + Config.IMAGE_TOURCAT_DEFAULT_UPLOAD + objTourCategory.Data.Images;
                    else objTourCategory.Data.Images = Config.IMAGE_NO_IMAGE;
                }
            }
            else
            {
                BindDataToRegionDropdown(0);
                imgImage.ImageUrl = Config.IMAGE_NO_IMAGE;
            }
        }
        catch
        {
            string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURCATEGORY;
            Response.Redirect(sLink);
        }
    }
    private void BindDataToCountryDropdown()
    {
        DataTable dtbCountry = new DataTable();
        Country objCountry = new Country();
        dtbCountry = objCountry.GetALL();

        ddlCountry.DataSource = dtbCountry;
        ddlCountry.DataValueField = "CountryID";
        switch (ConvertData.ConvertToInt(ddlLanguage.SelectedValue))
        {
            case 1:
                ddlCountry.DataTextField = "CountryName1";
                break;
            case 2:
                ddlCountry.DataTextField = "CountryName2";
                break;
            case 3:
                ddlCountry.DataTextField = "CountryName3";
                break;
        }
        ddlCountry.DataBind();
        Utl.SetNonSelectToControl(ddlCountry, MultipleLanguage.AllTextItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1], MultipleLanguage.AllValueItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1]);
    }
    private void BindDataToRegionDropdown(int nCountry)
    {
        DataTable dtbRegion = new DataTable();
        Region objRegion = new Region();
        dtbRegion = objRegion.SearchByCountry(nCountry);
        ddlRegion.DataSource = dtbRegion;
        ddlRegion.DataValueField = "RegionID";
        switch (ConvertData.ConvertToInt(ddlLanguage.SelectedValue))
        {
            case 1:
                ddlRegion.DataTextField = "RegionName1";
                break;
            case 2:
                ddlRegion.DataTextField = "RegionName2";
                break;
            case 3:
                ddlRegion.DataTextField = "RegionName3";
                break;
        }
        ddlRegion.DataBind();
        Utl.SetNonSelectToControl(ddlRegion, MultipleLanguage.DDL_SELECTVALUE[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1], MultipleLanguage.AllValueItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1]);
    }
    private eTourCategory TourCategoryEntity()
    {
        eTourCategory entityTourCategory = new eTourCategory();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            TourCategory objTransport = new TourCategory();
            objTransport.LoadById(nItemID);
            entityTourCategory = objTransport.Data;
        }
        return entityTourCategory;
    }
    private eTourCategory SetData()
    {
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);

        TourCategory objTourCategory = new TourCategory();
        eTourCategory eTourCategory = this.TourCategoryEntity();

        int nRegionID = ConvertData.ConvertToInt(ddlRegion.SelectedValue);
        objTourCategory.Data.RegionID = nRegionID;
        if (iptImage.Value != "")
        {
            Support.DeleteFile("TourCategory", eTourCategory.Images);
            objTourCategory.Data.Images = Support.UploadFile(iptImage, "TourCategory");
        }
        if (iptImage.Value == "")
        {
            if (eTourCategory.Images != "")
                objTourCategory.Data.Images = ConvertData.ConvertToString(eTourCategory.Images);
            else objTourCategory.Data.Images = "";
        }
        objTourCategory.Data.Parents = 0;
        switch (nCurrentLanguage)
        {
            case 1:
                objTourCategory.Data.TourCategoryName1 = txtTourCategoryName.Text.Trim();
                objTourCategory.Data.TourCategoryDetail1 = ConvertData.ConvertToString(FTBContent.RenderText());

                objTourCategory.Data.TourCategoryName2 = ConvertData.ConvertToString(eTourCategory.TourCategoryName2);
                objTourCategory.Data.TourCategoryDetail2 = ConvertData.ConvertToString(eTourCategory.TourCategoryDetail2);
                objTourCategory.Data.TourCategoryName3 = ConvertData.ConvertToString(eTourCategory.TourCategoryName3);
                objTourCategory.Data.TourCategoryDetail3 = ConvertData.ConvertToString(eTourCategory.TourCategoryDetail3);
                break;
            case 2:
                objTourCategory.Data.TourCategoryName2 = txtTourCategoryName.Text.Trim();
                objTourCategory.Data.TourCategoryDetail2 = ConvertData.ConvertToString(FTBContent.RenderText());

                objTourCategory.Data.TourCategoryName1 = ConvertData.ConvertToString(eTourCategory.TourCategoryName1);
                objTourCategory.Data.TourCategoryDetail1 = ConvertData.ConvertToString(eTourCategory.TourCategoryDetail1);
                objTourCategory.Data.TourCategoryName3 = ConvertData.ConvertToString(eTourCategory.TourCategoryName3);
                objTourCategory.Data.TourCategoryDetail3 = ConvertData.ConvertToString(eTourCategory.TourCategoryDetail3);
                break;
            case 3:
                objTourCategory.Data.TourCategoryName3 = txtTourCategoryName.Text.Trim();
                objTourCategory.Data.TourCategoryDetail3 = ConvertData.ConvertToString(FTBContent.RenderText());

                objTourCategory.Data.TourCategoryName2 = ConvertData.ConvertToString(eTourCategory.TourCategoryName2);
                objTourCategory.Data.TourCategoryDetail2 = ConvertData.ConvertToString(eTourCategory.TourCategoryDetail2);
                objTourCategory.Data.TourCategoryName1 = ConvertData.ConvertToString(eTourCategory.TourCategoryName1);
                objTourCategory.Data.TourCategoryDetail1 = ConvertData.ConvertToString(eTourCategory.TourCategoryDetail1);
                break;
        }

        return objTourCategory.Data;
    }
    #endregion

    #region Click Events
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        int nRegionID = ConvertData.ConvertToInt(ddlRegion.SelectedValue);
        if (nRegionID > 0)
        {
            TourCategory objTourCategory = new TourCategory();

            objTourCategory.Data = this.SetData();
            int nResult = 0;
            string sLink = "";
            string sAction = Request.QueryString[Config.ACTION];
            switch (sAction)
            {
                case Config.ACTION_ADD:
                    nResult = objTourCategory.Insert();
                    if (nResult > 0)
                    {
                        sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURCATEGORY + "&" + Config.MESS_ID + "=2";
                        Response.Redirect(sLink);
                    }
                    else Alert1.ShowMessage(2, MessagesAlert.INSERT_UNSUCCESSFUL[nDefaultLanguage]);
                    break;
                case Config.ACTION_EDIT:
                    int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                    nResult = objTourCategory.Update(nID);
                    if (nResult > 0)
                    {

                        Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);
                        if (objTourCategory.Data.Images.ToString() != "")
                        {
                            imgImage.ImageUrl = Config.ROOT + Config.IMAGE_TOURCAT_DEFAULT_UPLOAD + objTourCategory.Data.Images.ToString();
                        }
                        if (objTourCategory.Data.Images.ToString() == "")
                        {
                            objTourCategory.Data.Images = Config.ROOT + "/Images/no_image.gif";
                        }

                    }
                    else
                        Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage]);
                    break;
            }
        }
        else
        {
            Alert1.ShowMessage(2, MessagesValidate.REGION_SELECT[nDefaultLanguage]);
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURCATEGORY);

    }
    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        int nCountry = ConvertData.ConvertToInt(ddlCountry.SelectedValue);
        BindDataToRegionDropdown(nCountry);
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        BindDataWhenEdit();
    }
    #endregion

    #region BindDataToDDL
    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    #endregion
}